var UserSuggest = require('../../models/user/userSuggest')
var Active = require('../../models/active/active')

module.exports = {
    add: function(req, res) {
        var body = req.body

        new UserSuggest(body).save(function(err, apply) {
            if (err) {
                console.log('userSuggestController add fail. err = ' + err)
                return res.json({code: 177, msg: '提交失败'})
            }
            new Active({
                type: 3,
                active: body
            }).save(function (err, active) {
                if (err) {
                    console.log('userSuggestController add fail. err = ' + err)
                    return res.json({code: 177, msg: '提交失败'})
                }
                return res.json({code: 100, msg: '提交成功'})
            })
        })
    },
    getList: function (req, res) {

        var skip = + req.params.skip
        var limit = + req.params.limit

        var status = req.query.status
        var uid = req.query.userId

        var filter = {}
        if (status) {
            filter.status = + status
        }
        if (uid) {
            filter['handler.id'] = uid
        }

        UserSuggest.find(filter).sort({createdAt: -1}).skip(skip).limit(limit).lean().exec(function (err, suggests) {
            if (err) {
                console.log('userSuggestController getList fail. err = ' + err)
                return res.json({code: 177, msg: '获取失败'})
            }
            if (!suggests.length) {
                return res.json({code: 112, msg: '未找到数据'})
            }
            return res.json({code: 100, data: {list: suggests}})
        })

    },
    update: function (req, res) {
        var id = req.params.id

        var update = req.body
        update.status = 1

        UserSuggest.findOneAndUpdate({_id: id}, update, {upsert: true, new: true}, function (err, suggest) {
            if (err) {
                console.log('userSuggestController update fail. err = ' + err)
                return res.json({code: 177, msg: '更新失败'})
            }
            return res.json({code: 100, data: {suggest: suggest}})
        })

    }
}